<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="车id" prop="carId">
      <el-input v-model="dataForm.carId" placeholder="车id"></el-input>
    </el-form-item>
    <el-form-item label="司机id" prop="driverId">
      <el-input v-model="dataForm.driverId" placeholder="司机id"></el-input>
    </el-form-item>
    <el-form-item label="任务id" prop="taskId">
      <el-input v-model="dataForm.taskId" placeholder="任务id"></el-input>
    </el-form-item>
    <el-form-item label="数据时间" prop="dataTime">
      <el-input v-model="dataForm.dataTime" placeholder="数据时间"></el-input>
    </el-form-item>
    <el-form-item label="纬度" prop="lat">
      <el-input v-model="dataForm.lat" placeholder="纬度"></el-input>
    </el-form-item>
    <el-form-item label="经度" prop="lon">
      <el-input v-model="dataForm.lon" placeholder="经度"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          carId: '',
          driverId: '',
          taskId: '',
          dataTime: '',
          lat: '',
          lon: ''
        },
        dataRule: {
          carId: [
            { required: true, message: '车id不能为空', trigger: 'blur' }
          ],
          driverId: [
            { required: true, message: '司机id不能为空', trigger: 'blur' }
          ],
          taskId: [
            { required: true, message: '任务id不能为空', trigger: 'blur' }
          ],
          dataTime: [
            { required: true, message: '数据时间不能为空', trigger: 'blur' }
          ],
          lat: [
            { required: true, message: '纬度不能为空', trigger: 'blur' }
          ],
          lon: [
            { required: true, message: '经度不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sign/signcarlonlat/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.carId = data.signCarLonLat.carId
                this.dataForm.driverId = data.signCarLonLat.driverId
                this.dataForm.taskId = data.signCarLonLat.taskId
                this.dataForm.dataTime = data.signCarLonLat.dataTime
                this.dataForm.lat = data.signCarLonLat.lat
                this.dataForm.lon = data.signCarLonLat.lon
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sign/signcarlonlat/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'carId': this.dataForm.carId,
                'driverId': this.dataForm.driverId,
                'taskId': this.dataForm.taskId,
                'dataTime': this.dataForm.dataTime,
                'lat': this.dataForm.lat,
                'lon': this.dataForm.lon
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
